package cn.signalpha.modules.shop.controller.api;

import cn.hutool.core.map.MapUtil;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.enums.StatusEnum;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.jackson.JsonUtil;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.CollectionUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.entity.Goods;
import cn.signalpha.modules.shop.entity.Seller;
import cn.signalpha.modules.shop.service.GoodsService;
import cn.signalpha.modules.shop.service.SellerService;
import cn.signalpha.modules.shop.vo.SellerExtVO;
import cn.signalpha.modules.shop.vo.SellerListVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 控制器
 *
 * @author suguangjun
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/wap/seller")
@Api(value = "商户", tags = "商户")
public class ApiSellerController extends BaseController {

	private final GoodsService goodsService;
	private final SellerService sellerService;

	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="ids", dataType = "string", value = "逗号分隔"),
		@ApiImplicitParam(name="goodsNum", dataType = "string", value = "展示商品数量"),
		@ApiImplicitParam(name="orderByAsc", dataType = "string", value = "商品正序"),
		@ApiImplicitParam(name="orderByDesc", dataType = "string", value = "商品倒序"),
	})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "店铺街", notes = "")
	public R<IPage<SellerListVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		List<SellerListVO> sellerList = new ArrayList<>();
		params.put("status", StatusEnum.ON.getType());
		QueryWrapper<Seller> wrapper = sellerService.selectSellerWrapper(params);
		IPage<Seller> pages = sellerService.page(Condition.getPage(query), wrapper);
		pages.getRecords().forEach(seller -> {
			SellerListVO sellerListVO = Objects.requireNonNull(BeanUtil.copy(seller, SellerListVO.class));
			sellerListVO.setIsOpenPick(seller.getIsOpenPick());
			sellerListVO.setExtVO(JsonUtil.parse(seller.getExt(), SellerExtVO.class));
			sellerListVO.setScore(sellerService.score(seller.getId()));
			Map<String, Object> goodsParams = Kv.create().set("sellerId", seller.getId()).set("status", 1);
			goodsParams.put("orderByAsc", params.get("orderByAsc"));
			goodsParams.put("orderByDesc", params.get("orderByDesc"));
			QueryWrapper<Goods> goodsWrapper = goodsService.selectGoodsWrapper(goodsParams);
			goodsWrapper.select("id", "name", "img", "goods_price");
			goodsWrapper.lambda().last("limit " + Func.toStrWithEmpty(MapUtil.getStr(params, "goodsNum"), "3"));
			sellerListVO.setGoodsList(BeanUtil.copy(goodsService.list(goodsWrapper), SellerListVO.GoodsDTO.class));
			sellerList.add(sellerListVO);
		});
		List<Long> sellerIds = Func.toLongList(String.valueOf(params.get("ids")));
		if(CollectionUtil.isNotEmpty(sellerIds)){
			sellerList.sort(((o1, o2) -> {
				int io1 = sellerIds.indexOf(o1.getId());
				int io2 = sellerIds.indexOf(o2.getId());
				if (io1 != -1) {
					io1 = sellerList.size() - io1;
				}
				if (io2 != -1) {
					io2 = sellerList.size() - io2;
				}
				return io2 - io1;
			}));
		}
		IPage<SellerListVO> pageVo = new Page(pages.getCurrent(), pages.getSize(), pages.getTotal());
		pageVo.setRecords(sellerList);
		return R.data(pageVo);
	}

	@GetMapping("/info")
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", dataType = "string", value = "商户id"),
		@ApiImplicitParam(name="goodsNum", dataType = "string", value = "展示商品数量"),
		@ApiImplicitParam(name="orderByAsc", dataType = "string", value = "商品正序"),
		@ApiImplicitParam(name="orderByDesc", dataType = "string", value = "商品倒序"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "店铺详情", notes = "")
	public R<SellerListVO> info(@ApiIgnore @RequestParam Map<String, Object> params) {
		Seller seller = sellerService.getById(String.valueOf(params.get("id")));
		Assert.isNull(seller, MsgConstant.OBJECT_NULL);
		SellerListVO sellerListVO = Objects.requireNonNull(BeanUtil.copy(seller, SellerListVO.class));
		sellerListVO.setIsOpenPick(seller.getIsOpenPick());
		sellerListVO.setExtVO(JsonUtil.parse(seller.getExt(), SellerExtVO.class));
		sellerListVO.setScore(sellerService.score(seller.getId()));
		Map<String, Object> goodsParams = Kv.create().set("sellerId", seller.getId()).set("status", 1);
		goodsParams.put("orderByAsc", params.get("orderByAsc"));
		goodsParams.put("orderByDesc", params.get("orderByDesc"));
		QueryWrapper<Goods> goodsWrapper = goodsService.selectGoodsWrapper(goodsParams);
		goodsWrapper.select("id", "name", "img", "goods_price");
		goodsWrapper.lambda().last("limit " + Func.toStrWithEmpty(MapUtil.getStr(params, "goodsNum"), "3"));
		sellerListVO.setGoodsList(BeanUtil.copy(goodsService.list(goodsWrapper), SellerListVO.GoodsDTO.class));
		return R.data(sellerListVO);
	}

}
